package Servlets;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import sendMail.CreateMail;
import java.math.BigInteger;
import java.sql.SQLException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import MD5_Encryption.Encrypt;
import QueryExcecution.EnterBankDetails;
import QueryExcecution.enterPersonalDetails;
import beanUtils.BeanUtility;
import beans.BankDetailsBean;


public class AddBankDetails extends HttpServlet{
	BankDetailsBean bdb=new BankDetailsBean();
	CreateMail cm =new CreateMail();
	AddPersonalDetails apd=new AddPersonalDetails();
	enterPersonalDetails epd=new enterPersonalDetails();
	EnterBankDetails ebd=new EnterBankDetails();
	Encrypt e=new Encrypt();
	public String pass;
	private SecureRandom random = new SecureRandom();

	  public String nextSessionId()
	  {
	    pass= new BigInteger(40, random).toString(16);
	    
	    return pass;
	    
	  }


	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws IOException, ServletException {
		try{
			doPost(req, res);
		}
		catch(Exception e){
			
		}
	}///do get///////
	public void doPost(HttpServletRequest req, HttpServletResponse res){
		HttpSession ses=req.getSession();
		System.out.println("hello this is bank servlet.................");
		try {

		BeanUtility.populateBean(bdb, req);
		///////////     PERSONAL DETAILS     ////////////////////////////////////////////////////
		
		String fname=(String)ses.getAttribute("fname");
		String lname=(String)ses.getAttribute("lname");
		String otherocc=(String)ses.getAttribute("otherocc");
		String otherstatus=(String)ses.getAttribute("otherstatus");
		int homeno=(Integer)ses.getAttribute("homeno");
		int officeno=(Integer)ses.getAttribute("officeno");
		String outcity=(String)ses.getAttribute("outcity");
		String outaddress=(String)ses.getAttribute("outaddress");
		String status=(String)ses.getAttribute("status");
		int outpin=(Integer)ses.getAttribute("outpin");
		String occupation=(String)ses.getAttribute("occupation");
		String panno=(String)ses.getAttribute("panno");
		int mobileno=(Integer)ses.getAttribute("mobileno");
		int homepincode=(Integer)ses.getAttribute("homepincode");
		int faxno=(Integer)ses.getAttribute("faxno");
		String email=(String)ses.getAttribute("email");
		String homecity=(String)ses.getAttribute("homecity");
		String dob=(String)ses.getAttribute("dob");
		String homeaddress=(String)ses.getAttribute("homeaddress");
		int kyc_status=(Integer)ses.getAttribute("kyc_status");
		pass=nextSessionId();
		String enpass = null;
		try {
			enpass=e.encryptPassword(pass);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("the encrypted password is ----------"+enpass);
		int rows=epd.fillPersonalDetails(fname,lname,homeno,enpass,officeno,outcity,outaddress,status,outpin,occupation,panno,mobileno,homepincode,faxno,email,homecity,dob,homeaddress,kyc_status);
		
		System.out.println("personal detaisl are filled !!!!-------------------"+rows);
		///////////////   PERSONAL DETAILS ENDS ///////////////////////////////
		//----------------------------------------------------------------------------------------
		
		///////////////////////////  BANK DETAILS  //////////////////////////////////////////
		
		String bankname=bdb.getBankname();
		String branchname=bdb.getBankname();
		String accountno=bdb.getAccountno();
		String micr=bdb.getMicr();
		String rtgs=bdb.getRtgs();
		String accounttype=bdb.getAccounttype();
		String bankcity=bdb.getBankcity();
		
		int row=ebd.fillBankDetails(email,bankname,branchname,accountno,micr,rtgs,accounttype,bankcity);
		
		System.out.println("bank details are filled !!!! =================="+row);
		cm.send(email,pass);
		System.out.println("email has been sent !!! ");
		RequestDispatcher requestDispatcher = req
				.getRequestDispatcher("./index.jsp");
		requestDispatcher.forward(req, res);
		////////////////////  BANK DETAILS ENDS  ////////////////////////////////////////////////////////////
			
		}
		 catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 

	}///do post///////

		
}////class ends ///
